import { query } from '../services/manage';

export default {
  namespace: 'manage',
  state: {},
  effects: {
    *getCustomers({ payload }, { call, put }) {
      const { data: { message, customers } } = yield call(query, 'customers');
      message === 'success' ? payload.success() : payload.failed();
      yield put({ type: 'update', customers });
    },
    *getSuppliers({ payload }, { call, put }) {
      const { data: { message, suppliers } } = yield call(query, 'suppliers');
      message === 'success' ? payload.success() : payload.failed();
      yield put({ type: 'update', suppliers });
    },
    *getGoods({ payload }, { call, put }) {
      const { data: { message, goods } } = yield call(query);
      message === 'success' ? payload.success() : payload.failed();
      yield put({ type: 'update', goods });
    }
  },
  reducers: {
    update(state, action) {
      const newState = {
        customers: action.customers,
        suppliers: action.suppliers,
        goods: action.goods,
      }
      return { ...state, ...newState }
    }
  }
}